//
//  NetworkManager.swift
//  fho
//
//  Created by 郭颢源 on 2021/4/1.
//

import Foundation
import Moya

//import HandyJSON
//import Alamofire


private struct Target: TargetType {
    init(request: Requset) {
        self.request = request
    }
    var request: Requset
    var baseURL: URL {
        return URL(string: request.baseURL)!
    }
    var path: String {
        return request.path
    }
    
    var method: Moya.Method {
        return request.method == .post ? .post : .get
    }
    
    var sampleData: Data = "".data(using: .utf8)!
    
    var task: Task {
        return .requestParameters(parameters: request.parameters, encoding: URLEncoding.default)
    }
    var headers: [String : String]? {
        return ["Content-Type":"application/x-www-form-urlencoded"]
    }
}

class NetworkManager {
    static let shared = NetworkManager("")
    init(_ baseURL: String = "") {
        self.baseUrl = baseURL
    }
    var baseUrl = ""
    func post(path: String,params: [String: Any]?,result: @escaping ((Result<Data,Error>)->())){
        let request = Requset(baseURL:baseUrl, path: path, parameters: params)
        let target = Target(request: request)
        self.request(target: target, result: result)
    }
    func get(path: String,params: [String: Any]?,result: @escaping ((Result<Data,Error>)->())){
        let request = Requset(baseURL:baseUrl, path: path, parameters: params)
        let target = Target(request: request)
        self.request(target: target, result: result)
    }
    private func request(target: Target,result: @escaping ((Result<Data,Error>)->())) {
        MoyaProvider<Target>().request(target) { (res) in
            switch res {
            case .success(let a):
                result(.success(a.data))
            case .failure(let error):
                result(.failure(error))
            }
        }
    }
}
